import re

class M3uParser:
    title_pattern = re.compile(r'#EXTINF\s*:\s*((-?\d+)\s*,)?\s*(.*)')

    def __init__(self):
        pass

    def parse_file(self, file):
        streams = []
        stream = {}
        for line in file:
            line = line.strip()
            if not line or line == '#EXTM3U': continue
            match = M3uParser.title_pattern.match(line)
            if match:
                if match.group(1):
                    stream['length'] = match.group(2)
                stream['title'] = match.group(3)
            else:
                stream['file'] = line
                streams.append(stream)
                stream = {}
        return streams
